<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/10 0010
 * Time: 下午 10:47
 */

namespace App\System;


class ScanList
{
    private $commandList = [];

    private static $instance = null;

    /**
     * ScanList constructor.
     */
    private function __construct()
    {
        $this->init();

    }

    private function init($dir = "Command")
    {
        $dir = SRC_PATH . $dir . DS;

        if (!is_dir($dir)) {
            mkdir($dir);
        }
        $dirList = glob($dir . '*.php', GLOB_ERR);
        $this->pattenCommand($dirList);
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    /**
     * @return array
     */
    public function getCommandList()
    {
        return $this->commandList;
    }


    private function pattenCommand($dirList)
    {
        foreach ($dirList as $file) {
            $class = explode(DS, $file);
            $class = end($class);
            $class = substr($class,0,strpos($class,'.'));
            $ref = new \ReflectionClass("\App\Command\\".$class);
            $methods = $ref->getMethod("desc");
            $class= $methods->class;
            $method= $methods->name;
            $this->commandList[] = [$class,'run',$class::$method()];
        }
    }

}